import useGlobalAPI from '@/hooks/useGlobalAPI'
import {selectPmProgress, selectPmProgressById} from '@/api/myPerformance'
import {computed, ref} from 'vue'

export interface UseProgressArg {
  pmId: number
}

function useProgress(data?: UseProgressArg) {
  const {store} = useGlobalAPI()
  const loginUser = computed(() => store.getters['dingding/ddUserInfo'])

  let pmProgress = ref<MyPm.PmProgress[]>([])

  function setPmProgress(data: MyPm.PmProgress[]) {
    pmProgress.value = data
  }
  function updatePmProgress({pmId}: {pmId: number}) {
    selectPmProgress({pmId})
      .then(res => {
        res.data.sort((a, b) => a.sort - b.sort)
        setPmProgress(res.data)
      })
      .catch(err => {})
  }
  function getPmProgress({
    pmId,
    success,
    updatePmProgress = false
  }: {
    pmId: number
    updatePmProgress?: boolean
    success?: (data: MyPm.PmProgress[]) => void
  }) {
    selectPmProgress({pmId})
      .then(res => {
        if (updatePmProgress) {
          setPmProgress(res.data)
        }
        success && success(res.data)
      })
      .catch(err => {})
  }

  /**
   * 流程 当前正在执行的所有
   *
   * @desc 如果流程列表长度为0时，就返回undefined
   * */
  const nowRunning = computed(() => {
    let progressLen = pmProgress.value.length

    if (!progressLen) return undefined

    if (progressLen && pmProgress.value[progressLen - 1].pmProgressCompleteStatus === 2)
      return [pmProgress.value[progressLen - 1]]

    return pmProgress.value.filter(
      item => item.pmProgressCompleteStatus === 1
    ) as typeof pmProgress.value
  })

  /**
   * 流程 当前正在执行
   *
   * 当前登录者所属流程
   *
   * */
  const nowRunningByLoginuser = computed(() => {
    return nowRunning.value?.find(item => item.makeUserDDId === loginUser.value.userId)
  })

  /**流程 所有自评*/
  const allScoreSelf = computed(() => pmProgress.value.filter(item => item.pmProgressStep === 4))
  /**流程 所有自评 人员Id列表*/
  const allScoreSelfUserIds = computed(() => allScoreSelf.value.map(item => item.makeUserDDId))
  /**流程 所有自评 当前登陆者*/
  const scoreSelfByLoginUser = computed(() =>
    allScoreSelf.value.find(item => item.makeUserDDId == loginUser.value.userId)
  )

  /**流程 所有主管评分*/
  const allScoreLeader = computed(() => pmProgress.value.filter(item => item.pmProgressStep === 6))

  /**流程 所有主管评分 当前登陆者*/
  const scoreLeaderByLoginUser = computed(() =>
    allScoreLeader.value.find(item => item.makeUserDDId == loginUser.value.userId)
  )
  /**流程 所有主管评分 人员Id列表*/
  const allScoreLeaderUserIds = computed(() => allScoreLeader.value.map(item => item.makeUserDDId))

  /**流程 所有指定人员评分*/
  const allAppoint = computed(() =>
    pmProgress.value.filter(item => item.pmProgressStep === 7 && item.pmStageCode === '0018')
  )
  /**流程 所有指定人员评分 当前登陆者*/
  const appointByLoginUser = computed(() =>
    allAppoint.value.find(item => item.makeUserDDId == loginUser.value.userId)
  )
  /**流程 所有指定人员评分 人员Id列表*/
  const allAppointUserIds = computed(() => allAppoint.value.map(item => item.makeUserDDId))

  /**被考核人以及评分人Id列表 用于选择指定评分人时进行排除*/
  const excludeUserBySelectAppointUser = computed(() => [
    ...allScoreSelfUserIds.value,
    ...allScoreLeaderUserIds.value
  ])

  let progressById = ref<MyPm.PmProgress>()

  async function getProgressById({pmProgressId}: {pmProgressId: number}) {
    let res = await selectPmProgressById({pmProgressId})
    if (!res) return false
    progressById.value = res.data
    return res.data
  }

  return {
    updatePmProgress,
    getPmProgress,
    pmProgress,
    nowRunning,
    nowRunningByLoginuser,
    allScoreSelf,
    allAppoint,
    scoreSelfByLoginUser,
    loginUser,
    progressById,
    getProgressById,
    allScoreLeader,
    scoreLeaderByLoginUser,
    appointByLoginUser,
    allScoreSelfUserIds,
    allScoreLeaderUserIds,
    allAppointUserIds,
    excludeUserBySelectAppointUser
  }
}

export default useProgress
